package com.sailorj.medical.domain.ill_card;

import java.sql.Connection;

import com.sailorj.medical.domain.area.Area;
import com.sailorj.medical.domain.chronicdis.Chronicdis;
import com.sailorj.medical.util.MyTime;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

import com.sailorj.medical.database.DbUtil;
import com.sailorj.medical.domain.area.Area;
import com.sailorj.medical.domain.chronicdis.Chronicdis;
import com.sailorj.medical.exception.DbException;

public class Ill_card {
	private static final Logger LOG = LogManager.getLogger(Ill_card.class);
	public String ill_card_no;
	public String join_nh_code;
	public String IDcard;
	public String ill_code;
	private Area area;
	public Timestamp start_day;
	public Timestamp end_day;
	public String user_id;
	public Chronicdis chronicdis;

	public Ill_card() {
		super();
	}


	public void addIll_card(String ill_card_no, String join_nh_code, String IDcard, String ill_code, Area area,
			Timestamp start_day, Timestamp end_day, String user_id) throws Exception {
		Connection conn = null;
		try {
			// 初始化属性
			this.area = area;
			this.ill_card_no = join_nh_code + MyTime.getRandom(2);
			this.join_nh_code = join_nh_code;
			this.IDcard = IDcard;

			this.ill_code = ill_code;
			this.start_day = start_day;
			this.end_day = end_day;
			this.user_id = user_id;

			// 保存导数据库
			conn = DbUtil.getConn();
			conn.setAutoCommit(false);
			// 调用插入数据库私有方法
			this.saveToDB(conn);
			conn.commit();
		} catch (Exception e) {
			conn.rollback();
			throw e;
		} finally {
			DbUtil.close(conn);
		}

	}

	/**
	 * 插入记录
	 * 
	 * @param connection
	 * @throws SQLException
	 */
	private void saveToDB(Connection connection) throws SQLException {
		PreparedStatement pstmt = null;
		try {

			StringBuffer sqlBuff = new StringBuffer();
			sqlBuff.append(
					"insert into t_ill_card(ill_card_no, join_nh_code, IDcard, ill_code, areacode, start_day, end_day, user_id)");
			sqlBuff.append("values(?,?,?,?,?,?,?,?)");
			String sql = sqlBuff.toString();
			pstmt = connection.prepareStatement(sql);
			// 依次设置动态参数的值，注意顺序
			int index = 1;
			pstmt.setString(index++, this.ill_card_no);
			pstmt.setString(index++, this.join_nh_code);
			pstmt.setString(index++, this.IDcard);

			pstmt.setString(index++, this.ill_code);// 行政区域编码
			pstmt.setString(index++, this.area.getAreacode());// 行政区域编码
			pstmt.setTimestamp(index++, this.end_day);
			pstmt.setTimestamp(index++, this.start_day);
			pstmt.setString(index++, this.user_id);

			// 以executeUpdate()执行SQL
			pstmt.executeUpdate();
		} catch (SQLException e) {
			throw e;
		} finally {
			// 只关闭PrepareStatement资源
			if (pstmt != null) {
				pstmt.close();
			}

		}

	}

	/**
	 * 实例化对象
	 * 
	 * @param areacode
	 * @throws DbException
	 */
	public Ill_card(String ill_card_no) throws DbException {
		if (ill_card_no == null) {
			throw new IllegalStateException("家庭档案信息编号不能空");
		}
		try {
			this.ill_card_no = ill_card_no;
			loadDB();
		} catch (SQLException e) {
			throw new DbException("加载id=" + this.getIll_card_no() + "家庭档案信息记录失败", e);
		}
	}

	/**
	 * 
	 * @param areaid
	 * @throws SQLException
	 */
	private void loadDB() throws SQLException {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DbUtil.getConn();
			pstmt = conn.prepareStatement("select * from t_ill_card where ill_card_no=?");
			int index = 1;
			pstmt.setString(index, this.getIll_card_no());
			rs = pstmt.executeQuery();
			if (rs.next()) {
				this.ill_code = rs.getString("ill_code");
			}
		} catch (SQLException e) {
			throw e;
		} finally {
			DbUtil.close(rs, pstmt, conn);
		}
	}

	public void edit() throws SQLException {
		Connection conn = DbUtil.getConn();
		try {
			conn.setAutoCommit(false);
			this.updateToDB(conn);
			conn.commit();
		} catch (SQLException e) {
			conn.rollback();
			throw e;
		} finally {
			DbUtil.close(conn);
		}
	}

	private void updateToDB(Connection conn) throws SQLException {
		PreparedStatement pstmt = null;
		LOG.info("upDB=" + ill_card_no);
		try {
			// 定义构造update SQL语句字符串变量sql
			String sql = "update t_ill_card set ill_code=? where ill_card_no=?";
			// 创建执行带动态参数的SQL的PreparedStatement pstmt
			pstmt = conn.prepareStatement(sql);
			// 设置动态参数对应的值
			int index = 1;
			pstmt.setString(index++, this.ill_code);
			pstmt.setString(index++, this.ill_card_no);
			int count = pstmt.executeUpdate();

		} catch (SQLException e) {
			throw e;
		} finally {
			DbUtil.close(pstmt);
		}

	}

	public void del(String ill_card_no) throws SQLException {
		LOG.info("del=" + ill_card_no);
		Connection conn = null;
		try {
			// 1:对象属性赋值
			this.ill_card_no = ill_card_no;
			conn = DbUtil.getConn();
			// 2；调用delFromDB方法
			delFromDB(conn);
		} catch (SQLException e) {
			throw e;
		} finally {
			DbUtil.close(conn);
		}

	}

	private void delFromDB(Connection conn) throws SQLException {
		PreparedStatement pstmt = null;
		try {
			LOG.info("delDB=" + ill_card_no);
			// 定义构造插入SQL语句字符串变量sql
			String sql = "delete from t_ill_card where ill_card_no=?";
			// 创建执行带动态参数的SQL的PreparedStatement pstmt
			pstmt = conn.prepareStatement(sql);
			// 设置动态参数对应的值
			int index = 1;
			pstmt.setString(index++, this.ill_card_no);

			int count = pstmt.executeUpdate();

		} catch (SQLException e) {
			throw e;
		} finally {
			DbUtil.close(pstmt);

		}

	}

	public Ill_card(String ill_card_no, String join_nh_code, String iDcard, String ill_code, Area area,
			Timestamp start_day, Timestamp end_day, String user_id, Chronicdis chronicdis) {
		super();
		this.ill_card_no = ill_card_no;
		this.join_nh_code = join_nh_code;
		IDcard = iDcard;
		this.ill_code = ill_code;
		this.area = area;
		this.start_day = start_day;
		this.end_day = end_day;
		this.user_id = user_id;
		this.chronicdis = chronicdis;
	}

	public Ill_card(String ill_card_no, String ill_code) {
		super();
		this.ill_card_no = ill_card_no;
		this.ill_code = ill_code;
	}

	public String getIll_card_no() {
		return ill_card_no;
	}

	public void setIll_card_no(String ill_card_no) {
		this.ill_card_no = ill_card_no;
	}

	public String getJoin_nh_code() {
		return join_nh_code;
	}

	public void setJoin_nh_code(String join_nh_code) {
		this.join_nh_code = join_nh_code;
	}

	public String getIDcard() {
		return IDcard;
	}

	public void setIDcard(String iDcard) {
		IDcard = iDcard;
	}

	public String getIll_code() {
		return ill_code;
	}

	public void setIll_code(String ill_code) {
		this.ill_code = ill_code;
	}

	public Area getArea() {
		return area;
	}

	public void setArea(Area area) {
		this.area = area;
	}

	public Timestamp getStart_day() {
		return start_day;
	}

	public void setStart_day(Timestamp start_day) {
		this.start_day = start_day;
	}

	public Timestamp getEnd_day() {
		return end_day;
	}

	public void setEnd_day(Timestamp end_day) {
		this.end_day = end_day;
	}

	public String getUser_id() {
		return user_id;
	}

	public void setUser_id(String user_id) {
		this.user_id = user_id;
	}

	public Chronicdis getChronicdis() {
		return chronicdis;
	}

	public void setChronicdis(Chronicdis chronicdis) {
		this.chronicdis = chronicdis;
	}

}
